/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.dao.impl;

import cz.muni.fi.pa165.bookingmanager.entities.Hotel;
import cz.muni.fi.pa165.bookingmanager.exceptions.IllegalOrphanException;
import cz.muni.fi.pa165.bookingmanager.exceptions.NonexistentEntityException;
import cz.muni.fi.pa165.bookingmanager.dao.HotelManager;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Štefan Repček
 */
@Stateless
public class HotelManagerImpl implements Serializable, HotelManager {
    private static final long serialVersionUID = 1L;

    @PersistenceContext(unitName = "BookingManagerPU")
    private EntityManager em;

    @Override
    public void createHotel(Hotel hotel) throws IllegalOrphanException {

        if (hotel == null) {
            throw new IllegalArgumentException();
        }

        List<String> illegalOrphanMessages = null;
        if (hotel.getId() != null) {
            illegalOrphanMessages = new ArrayList<String>();
            illegalOrphanMessages.add("Hotel id is not null,Hotel cant be added to database ");
            throw new IllegalOrphanException(illegalOrphanMessages);
        }



        em.persist(hotel);


    }

    @Override
    public void updateHotel(Hotel hotel) throws NonexistentEntityException, IllegalOrphanException, Exception {

        if (hotel == null) {
            throw new IllegalArgumentException();
        }
        try {

            List<String> illegalOrphanMessages = null;
            if (hotel.getId() == null) {
                illegalOrphanMessages = new ArrayList<String>();
                illegalOrphanMessages.add("Hotel id is null");
                throw new IllegalOrphanException(illegalOrphanMessages);
            }



            if (em.find(Hotel.class, hotel.getId()) == null) {
                throw new NonexistentEntityException("The hotel with id " + hotel.getId() + " no longer exists.");
            }

            hotel = em.merge(hotel);

        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();

            throw ex;
        }

    }

    @Override
    public void deleteHotel(Hotel hotel) throws NonexistentEntityException, IllegalOrphanException {

        if (hotel == null) {
            throw new IllegalArgumentException();
        }
        List<String> illegalOrphanMessages = null;
        if (hotel.getId() == null) {
            illegalOrphanMessages = new ArrayList<String>();
            illegalOrphanMessages.add("Hotel id is null");
            throw new IllegalOrphanException(illegalOrphanMessages);
        }




        if (em.find(Hotel.class, hotel.getId()) == null) {
            throw new NonexistentEntityException("The hotel with id " + hotel.getId() + " no longer exists.");
        }


        em.remove(em.merge(hotel));

        hotel.setId(null);


    }

    @Override
    public Hotel getHotelById(Long id) {
        if (id == null) {
            throw new IllegalArgumentException();
        }


        return em.find(Hotel.class, id);

    }

    @Override
    public List<Hotel> findAllHotels() {


        Query q = em.createQuery("select object(o) from Hotel as o");
        return q.getResultList();

    }

    @Override
    public List<Hotel> findHotelsByName(String name) {
        if (name == null) {
            throw new IllegalArgumentException();
        }

        Query q = em.createQuery("SELECT o FROM Hotel o WHERE o.name = '" + name + "'");
        return q.getResultList();

    }

    @Override
    public List<Hotel> findHotelsByCity(String city) {
        if (city == null) {
            throw new IllegalArgumentException();
        }

        Query q = em.createQuery("SELECT o FROM Hotel o WHERE o.city = '" + city + "'");
        return q.getResultList();

    }
}
